package com.cheejee.goodscenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cheejee.goodscenter.dao.GoodsCategoryPropertiesMapper;
import com.cheejee.goodscenter.module.entity.GoodsCategoryProperties;
import com.cheejee.goodscenter.service.GoodsCategoryPropertiesService;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author CARRY
 * @Date 2020年10月28日上午1:28:38
 */
@Service
public class GoodsCategoryPropertiesServiceImpl implements GoodsCategoryPropertiesService {

	@Resource
	private GoodsCategoryPropertiesMapper gcpm;

	@Override
	public GoodsCategoryProperties queryById(Long id) {

		return gcpm.selectByPrimaryKey(id);
	}

	@Override
	public Page<GoodsCategoryProperties> queryGoodsCategoryProperties(Long goodsId, Integer categoryPropertiesTemplateId, String propertiesName, int pageNum, int pageSize) {

		return PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> gcpm.selectBySelective(goodsId, categoryPropertiesTemplateId, propertiesName));
	}

	@Override
	@Check(message = "新增的商品分类属性不能为空", group = InsertGroup.class)
	public int addGoodsCategoryProperties(GoodsCategoryProperties properties) {

		return gcpm.insertSelective(properties);
	}
	
	@Override
	@Check(message = "新增的商品分组属性不能为空", group = InsertGroup.class)
	public int addGoodsCategoryProperties(List<GoodsCategoryProperties> properties) {
		
		return gcpm.insertMany(properties);
	}

	@Override
	public int changeGoodsCategoryPropertiesValue(Long propertiesId, String propertiesValue){
		checkExist(propertiesId);
		GoodsCategoryProperties properties = new GoodsCategoryProperties();
		properties.setId(propertiesId);
		properties.setPropertiesValue(propertiesValue);

		return gcpm.updateByPrimaryKeySelective(properties);
	}

	@Override
	public int deleteGoodsCategoryProperties(Long... id) {

		return gcpm.deleteById(id);
	}
	private void checkExist(Long id) {
		if(gcpm.selectByPrimaryKey(id) == null) {
			throw new DataNotExistException("商品分类属性不存在");
		}
	}
}
